<?php
class ModelCatalogCategory extends Model {
	public function getCategory($category_id) {
		$query = $this->db->query("SELECT DISTINCT * FROM " . DB_PREFIX . "category c LEFT JOIN " . DB_PREFIX . "category_description cd ON (c.category_id = cd.category_id) LEFT JOIN " . DB_PREFIX . "category_to_store c2s ON (c.category_id = c2s.category_id) WHERE c.category_id = '" . (int)$category_id . "' AND cd.language_id = '" . (int)$this->config->get('config_language_id') . "' AND c2s.store_id = '" . (int)$this->config->get('config_store_id') . "' AND c.status = '1'");
		 
		return $query->row;
	}
    
	public function getCategories($parent_id = 0) {

		$category_data = $this->cache->get('category.' . $parent_id . '.' . $this->config->get('config_language_id') . '.' . (int)$this->config->get('config_store_id'));

		if (!$category_data) {
			$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "category c LEFT JOIN " . DB_PREFIX . "category_description cd ON (c.category_id = cd.category_id) LEFT JOIN " . DB_PREFIX . "category_to_store c2s ON (c.category_id = c2s.category_id) WHERE c.parent_id = '" . (int)$parent_id . "' AND cd.language_id = '" . (int)$this->config->get('config_language_id') . "' AND c2s.store_id = '" . (int)$this->config->get('config_store_id') . "'  AND c.status = '1' AND c.sort_order <> '-1' ORDER BY c.sort_order, LCASE(cd.name)");

			$category_data = $query->rows;

			$this->cache->set('category.' . $parent_id . '.' . $this->config->get('config_language_id') . '.' . (int)$this->config->get('config_store_id'), $category_data);
		}

		return $category_data; 
	}
		
    public function getDisableCategories($parent_id = 0) {

        $category_data = $this->cache->get('disable_category.' . $parent_id . '.' . $this->config->get('config_language_id') . '.' . (int)$this->config->get('config_store_id'));

        if (!$category_data) {
            $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "category c LEFT JOIN " . DB_PREFIX . "category_description cd ON (c.category_id = cd.category_id) LEFT JOIN " . DB_PREFIX . "category_to_store c2s ON (c.category_id = c2s.category_id) WHERE c.parent_id = '" . (int)$parent_id . "' AND cd.language_id = '" . (int)$this->config->get('config_language_id') . "' AND c2s.store_id = '" . (int)$this->config->get('config_store_id') . "'  AND c.status = '0' AND c.sort_order <> '-1' ORDER BY c.sort_order, LCASE(cd.name)");

            $category_data = $query->rows;

            $this->cache->set('disable_category.' . $parent_id . '.' . $this->config->get('config_language_id') . '.' . (int)$this->config->get('config_store_id'), $category_data);
        }

        return $category_data;
    }
    		
	public function getTotalCategoriesByCategoryId($parent_id = 0) {
		$query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "category c LEFT JOIN " . DB_PREFIX . "category_to_store c2s ON (c.category_id = c2s.category_id) WHERE c.parent_id = '" . (int)$parent_id . "' AND c2s.store_id = '" . (int)$this->config->get('config_store_id') . "' AND c.status = '1' AND c.sort_order <> '-1'");
		
		return $query->row['total'];
	}
	
	public function getParentCategory($category_id) {
		$query = $this->db->query("SELECT parent_id FROM " . DB_PREFIX . "category WHERE category_id = '" . (int)$category_id . "'"); 
		
		return !empty($query->row) ? $query->row['parent_id'] : 0;
	}
    
    public function getCategoryName($category_id) {
        $query = $this->db->query("SELECT DISTINCT cd.`name` FROM " . DB_PREFIX . "category c LEFT JOIN " . DB_PREFIX . "category_description cd ON (c.category_id = cd.category_id) LEFT JOIN " . DB_PREFIX . "category_to_store c2s ON (c.category_id = c2s.category_id) WHERE c.category_id = '" . (int)$category_id . "' AND cd.language_id = '" . (int)$this->config->get('config_language_id') . "' AND c2s.store_id = '" . (int)$this->config->get('config_store_id') . "' AND c.status = '1'");
         
        return $query->row['name'];
    }
    
    public function getParentCategoryName($category_id) {
        $query = $this->db->query("SELECT parent_id FROM " . DB_PREFIX . "category WHERE category_id = '" . (int)$category_id . "'"); 
         
        return $query->row['name'];
    }
    
    public function createBreadcrumbs($categories_id){ 
        $categories_sql = is_array($categories_id) ? implode(',', $categories_id) : $categories_id;
        
        if(is_array($categories_id) && $categories_sql != ''){            
            $sql = "SELECT DISTINCT c.category_id, cd.`name` FROM " . DB_PREFIX . "category c " .
                " LEFT JOIN " . DB_PREFIX . "category_description cd ON (c.category_id = cd.category_id) " .
                " LEFT JOIN " . DB_PREFIX . "category_to_store c2s ON (c.category_id = c2s.category_id) " .
                " WHERE c.category_id IN(" . $categories_sql . ") AND cd.language_id = '" . (int)$this->config->get('config_language_id') . "' AND c2s.store_id = '" . (int)$this->config->get('config_store_id') . "' AND c.status = '1'";            
        }
        else{
            $sql = "SELECT DISTINCT c.category_id, cd.`name` FROM " . DB_PREFIX . "category c " .
                " LEFT JOIN " . DB_PREFIX . "category_description cd ON (c.category_id = cd.category_id) " .
                " LEFT JOIN " . DB_PREFIX . "category_to_store c2s ON (c.category_id = c2s.category_id) " .
                " WHERE c.category_id = '" . (int)$categories_sql . "' AND cd.language_id = '" . (int)$this->config->get('config_language_id') . "' AND c2s.store_id = '" . (int)$this->config->get('config_store_id') . "' AND c.status = '1'";    
        }          
        $query = $this->db->query($sql);    
        $categories = $query->rows;
        
        $breadcrumbs = array();
        $breadcrumb_path = '';
        foreach($categories_id as $category_id){
            $separator = $breadcrumb_path ? ' >> '  : '';
            $breadcrumb_path = $breadcrumb_path ? ($breadcrumb_path . '_' . $category_id) : $category_id;
            
            $category_name = '';
            foreach($categories as $item){
                if($item['category_id'] == $category_id){
                    $category_name = $item['name'];
                    break;    
                }
            }
            
            if($breadcrumb_path != $category_id){
                $url = HTTP_SERVER . 'index.php?route=product/category&path=' . $breadcrumb_path;    
            }
            else{
                $url = HTTP_SERVER . 'index.php?route=product/categories&path=' . $breadcrumb_path;
            }
            
            $breadcrumbs[] = array(
                'href'      => $url,
                'text'      => $category_name,
                'separator' => $separator,
            );
        } 
        
        return $breadcrumbs;          
    }
}
?>